<template>
  <el-drawer v-model="drawerVisible" :z-index="1000" :destroy-on-close="true" size="500px" :title="`${drawerProps.title}`">
    <el-form
      ref="terminalFormRef"
      label-width="130px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="终端名称" prop="termName">
        <el-input v-model="drawerProps.row!.termName" placeholder="请填终端名称" clearable></el-input>
      </el-form-item>

      <el-form-item label="终端编号" prop="termNo">
        <el-input v-model="drawerProps.row!.termNo" placeholder="请填写终端编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属装置" prop="deviceId">
        <el-select v-model="drawerProps.row!.deviceId" placeholder="请选择所属装置" clearable @change="_selectDeivce">
          <el-option v-for="item in deviceSelList" :key="item.deviceId" :label="item.deviceName" :value="item.deviceId" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属工位" prop="stationId">
        <el-select
          v-model="drawerProps.row!.stationId"
          placeholder="请选择所属工位"
          :disabled="!drawerProps.row!.deviceId"
          clearable
        >
          <el-option v-for="item in stationSelList" :key="item.stationId" :label="item.stationName" :value="item.stationId" />
        </el-select>
      </el-form-item>
      <el-form-item label="终端类型" prop="termType">
        <el-select v-model="drawerProps.row!.termType" placeholder="请选择终端类型" clearable>
          <el-option v-for="item in termType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="终端地址" prop="termAddress">
        <el-input v-model="drawerProps.row!.termAddress" placeholder="请填写终端地址" clearable></el-input>
      </el-form-item>
      <el-form-item label="接线方式" prop="linestyle">
        <el-select v-model="drawerProps.row!.linestyle" placeholder="请选择接线方式" clearable>
          <el-option v-for="item in linestyle" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="通讯规约" prop="protocol">
        <el-select v-model="drawerProps.row!.protocol" placeholder="请选择通讯规约" clearable>
          <el-option v-for="item in termProtocol" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="通讯方式" prop="commType">
        <el-select v-model="drawerProps.row!.commType" placeholder="请选择通讯方式" clearable>
          <el-option v-for="item in commType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="终端IP地址" prop="termIp">
        <el-input v-model="drawerProps.row!.termIp" placeholder="请填写终端IP地址" clearable></el-input>
      </el-form-item>
      <el-form-item label="终端端口号" prop="termPort">
        <el-input v-model="drawerProps.row!.termPort" placeholder="请填写终端端口号" clearable></el-input>
      </el-form-item>
      <el-form-item label="串口通讯波特率" prop="baudrate">
        <el-select v-model="drawerProps.row!.baudrate" placeholder="请选择串口通讯波特率" clearable>
          <el-option v-for="item in baudrate" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="_colse">取消</el-button>
      <el-button type="primary" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="TerminalDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Terminal, ResultData } from "@/api/interface";
import { termType, linestyle, termProtocol, commType, baudrate } from "@/utils/serviceDict";
import { getCabinetSelList } from "@/api/modules/files1Manage/cabinet";
import { getStationSelList } from "@/api/modules/files1Manage/station";
const validateNumber = (rule: any, value: string, callback: (arg0?: Error | undefined) => void) => {
  if (!/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/.test(value)) {
    callback(new Error("请输入数字值"));
  } else {
    callback();
  }
};
const rules = reactive({
  termName: [{ required: true, trigger: "blur", message: "请填终端名称" }],
  termNo: [{ required: true, trigger: "blur", message: "请填写终端编号" }],
  deviceId: [{ required: true, trigger: "blur", message: "请选择所属装置" }],
  stationId: [{ required: true, trigger: "blur", message: "请选择所属工位" }],
  termType: [{ required: true, trigger: "blur", message: "请选择终端类型" }],
  commType: [{ required: true, trigger: "blur", message: "请选择通讯方式" }],
  protocol: [{ required: true, trigger: "blur", message: "请选择通讯规约" }],
  linestyle: [{ required: true, trigger: "blur", message: "请选择接线方式" }],
  baudrate: [{ required: true, trigger: "blur", message: "请选择串口通讯波特率" }],
  termIp: [
    { required: true, trigger: "blur", message: "请填写终端IP地址" },
    {
      required: true,
      trigger: "blur",
      pattern: /([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])(\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])){3}$/,
      message: "终端ip地址格式不正确"
    }
  ],
  termPort: [{ required: true, trigger: "blur", message: "请填写终端端口号" }],
  termAddress: [
    { required: true, trigger: "blur", message: "请填写终端地址" },
    { validator: validateNumber, trigger: "blur", message: "终端地址必须由数字组成" }
  ]
});

const deviceSelList: any = ref([]);
const stationSelList: any = ref([]);

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Terminal.ResTerminalInfoList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  resetTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});
/**
 * 选择所属装置
 */
const _selectDeivce = async (deviceId: number) => {
  if (!deviceId) return;
  const stationRes = (await getStationSelList({
    pageNum: 1,
    pageSize: 100000000,
    deviceId
  })) as ResultData;

  stationSelList.value = stationRes.result.dataList;
  drawerProps.value.row.stationId = "";
};
// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  const { result } = (await getCabinetSelList({ pageNum: 1, pageSize: 100000000 })) as ResultData;
  deviceSelList.value = result.dataList;
};

// 提交数据（新增/编辑）
const terminalFormRef = ref<FormInstance>();
/**
 * 关闭弹窗
 */
const _colse = () => {
  drawerVisible.value = false;
  terminalFormRef.value && terminalFormRef.value.resetFields();
};
/**
 * 提交信息
 */
const handleSubmit = () => {
  terminalFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!({ ...drawerProps.value.row });
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.title === "新增" ? drawerProps.value.resetTableList!() : drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};
defineExpose({
  acceptParams
});
</script>
